package com.droidclaimsurveyor.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.droidclaimsurveyor.GCMIntentService;
import com.droidclaimsurveyor.R;
import com.droidclaimsurveyor.bl.DroidClaimBlService;
import com.droidclaimsurveyor.dto.AuthenSurveyorBlDto;
import com.droidclaimsurveyor.dto.AuthenSurveyorResultBlDto;
import com.droidclaimsurveyor.dto.SurveyorBlDto;
import com.droidclaimsurveyor.helper.asyntask.ICompleteListener;
import com.droidclaimsurveyor.helper.utils.DownloadImage;
import com.google.android.gcm.GCMRegistrar;

public class LoginActivity extends Activity implements ICompleteListener {
	private EditText surveyorIdET;
	private EditText passwordET;
	private String loginTaskId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		RegisterWithGCM();

	}

	private void RegisterWithGCM() {
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		GCMIntentService.registrationId = GCMRegistrar.getRegistrationId(this);
		if (GCMIntentService.registrationId.equals("")) {
			GCMRegistrar.register(this, "344698691373"); // Note: get the sender
															// id from
															// configuration.
		} else {

			Log.v("Registration", "Already registered, registrationId: "
					+ GCMIntentService.registrationId);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	public void handleClick(View target) {
		if (target.getId() == R.id.btnLogin) {
			surveyorIdET = (EditText) findViewById(R.id.surveyor_id_ET);
			String username = surveyorIdET.getText().toString();
			passwordET = (EditText) findViewById(R.id.password_ET);
			String user_Password = passwordET.getText().toString();
			AuthenSurveyorBlDto auth_dto = new AuthenSurveyorBlDto();
			auth_dto.setUsername(username);
			auth_dto.setPassword(user_Password);
			auth_dto.setRegistrationId(GCMIntentService.registrationId);
			auth_dto.setNoSurveyorData(true);
			DroidClaimBlService bl = new DroidClaimBlService(this);
			loginTaskId = bl.AuthenSurveyor(auth_dto);

		}
	}

	@Override
	public void onTaskCompleteSuccess(String taskId, Object result) {
		if(taskId.equals(loginTaskId)) {
			AuthenSurveyorResultBlDto dto = (AuthenSurveyorResultBlDto) result;
			if (dto.getSessionId() != null && !dto.getSessionId().isEmpty()) {
				SurveyorBlDto surv_dto = dto.getSurveyorInfo();

				MainActivity.setSessionId(dto.getSessionId());
				//MainActivity.setSessionId(surv_dto.getUsername());
				MainActivity.setOwnerName(surv_dto.getName() + " "
						+ surv_dto.getSurname());

				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				
				intent.putExtra("surveyerId", surv_dto.getSurveyerId());
				intent.putExtra("name", surv_dto.getName());
				intent.putExtra("surname", surv_dto.getSurname());
				intent.putExtra("mobile", surv_dto.getMobile());
				intent.putExtra("email", surv_dto.getEmail());
				intent.putExtra("imgPath", surv_dto.getPicture());
				intent.putExtra("status", true);
				
				startActivity(intent);
			} else {
				Toast.makeText(this, "Invalid login" + taskId,
						Toast.LENGTH_LONG).show();
			}

		}

	}

	@Override
	public void onTaskFailed(String taskId, Exception cause) {
		// TODO Auto-generated method stub
		//if (taskId == loginTaskId) {
			Toast.makeText(this, "Error" + taskId, Toast.LENGTH_LONG).show();

		//}
	}
}
